from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer as TimeSerializer, BadData, Serializer

from apps.users.models import User


def generagt_verify_email_url(user):
    serializer = TimeSerializer(settings.SECRET_KEY,expires_in=3600)

    data = {'user_id':user.id,'email':user.email}

    token = serializer.dumps(data).decode()

    verify_url = settings.EMAIL_VERIFY_URL+'?token='+token

    return verify_url

def check_verify_email_token(token):
    """
    验证token并提取user
    :param token: 用户信息签名后的结果
    :return: user, None
    """
    serializer = Serializer(settings.SECRET_KEY, expires_in=3600)
    try:
        data = serializer.loads(token)
    except BadData:
        return None
    else:
        user_id = data.get('user_id')
        email = data.get('email')
        try:
            user = User.objects.get(id=user_id, email=email)
        except User.DoesNotExist:
            return None
        else:
            return user
